package com.hotel.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.gson.Gson;
import com.hotel.BL.HouseKeepingLogic;

import data.HouseKeeping;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class HouseKeepingService extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String func = req.getParameter("func");
			if (func.equals("UpdateHouseKeeping")) {
				UpdateHouseKeeping(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void UpdateHouseKeeping(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		Gson gson = new Gson();
		HttpSession session=req.getSession(true);
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) session.getAttribute("hotelId");
		String[] roomIdString = req.getParameter("roomId").split(",");
		String ref = req.getParameter("ref");
		String userId = req.getParameter("userId");
		HouseKeepingLogic hkLogic = new HouseKeepingLogic(req,resp);
		
		List<HouseKeeping> result = new ArrayList<HouseKeeping>();
		for(int i=0;i<roomIdString.length;i++){
			long roomId = Long.parseLong(roomIdString[i]);
			HouseKeeping rs = hkLogic.UpdateCurrentHouseKeeping(hotelId, roomId, ref, userId);
			result.add(rs);
		}
		String json = (result.size()==1)? gson.toJson(result.get(0)): gson.toJson(result);
		
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(json);
	}
}